// pages/shopManager/shopService/serviceClassify/serviceClassify.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classifyList: [],
        addClassifyShow: false,
        classifyName: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.setData({
            classifyList: [],
            addClassifyShow: false,
            classifyName: ''
        })
        this.getClassifyList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            classifyList: [],
            addClassifyShow: false,
            classifyName: ''
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getClassifyList: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            show_type: 2,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                classifyList: res
            })
        }).catch(function(e) {})
    },

    goClassifyGoods: function(e) {
        wx.navigateTo({
            url: '/pages/shopManager/shopService/serviceList/serviceList?classifyId=' + e.currentTarget.dataset.id
        })
    },

    goClassifyEdit: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopService/classifyEdit/classifyEdit'
        })
    },

    goAddClassify: function() {
        this.setData({
            addClassifyShow: true
        })
    },

    myCatchTouch: function() {
        return
    },

    cancleClassifyShow: function() {
        this.setData({
            addClassifyShow: false
        })
    },

    classifyNameInput: function(e) {
        this.setData({
            classifyName: e.detail.value
        })
    },

    sureAddClassify: function() {
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            show_type: 2,
            class_name: this.data.classifyName
        }
        common.post('Goodsclass/store_add_class', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                that.setData({
                    addClassifyShow: false,
                    addClassifyName: ''
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '成功新建分类',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            addClassifyShow: false,
                            addClassifyName: ''
                        })
                        that.getClassifyList()
                    }
                }
            })
        }).catch(function(e) {})
    }
})